<?php

namespace Admin\Controller;

use Think\Exception;

/*
create table banner
(
    id          int auto_increment primary key,
    title       char(20)     not null default '' comment '标题',
    head_img    varchar(255) not null default '' comment '图片url',
    url         varchar(255) not null default '' comment '跳转url',
    type_code   char(15)     not null default 0 comment '代码',
    type_name   char(15)     not null default 0 comment '代码释义',
    create_time datetime comment '创建时间',
    update_time timestamp comment '修改时间'
) comment '轮播图表';
 * */

class BannerController extends BaseController
{
    public function _initialize()
    {
        if (method_exists(get_parent_class(), '_initialize')) {
            parent::_initialize(); // TODO: Change the autogenerated stub
        }
        $this->model = 'banner';
    }

    /*列表
     *
     * */
    public function index()
    {
        try {
            /*----------------------------------------凑查询条件-------------------------------------------------------*/
            $where = array();


            $where = array_filter($where);
            /*---------------------------------------开始查询----------------------------------------------------------*/
            $model = D($this->model);
            $data = $model
                ->where($where)
                ->select();
            if (!is_array($data)) {
                throw new Exception('查询失败!' . $model->getError(), 1);
            }
            $this->arr['msg'] = I('msg') ? I('msg') : '查询成功';
            $this->arr['code'] = 1;
            $this->arr['data'] = $data;
        } catch (Exception $e) {
            $this->arr['msg'] = $e->getMessage();
        }
        $this->ajaxReturn($this->arr);
    }

    /*更新
     *
     * */
    public function update()
    {
        try {
            $id = I('id');
            if (!$id) {
                throw new Exception('缺少id!', 1);
            }
            $model = D($this->model);
            $data = $model->find($id);
            if (!is_array($data) || empty($data)) {
                throw new Exception('id有误,或数据为空', 2);
            }
            if (IS_GET) {
                $this->arr['data'] = $data;
            } elseif (IS_POST) {
                if (!$model->create() || !$model->where(array('id' => $id))->save()) {
                    throw new Exception('保存出错!', 3);
                }
                $this->arr['code'] = 1;
                $this->arr['msg'] = I('msg') ? I('msg') : '保存成功!' . $model->getLastInsID();
            } else {
                $this->arr['msg'] = '仅接受get和post传输, get查询,post提交! 其他方式一律不接受!';
            }
        } catch (Exception $e) {
            $this->arr['msg'] = $e->getMessage();
        }
        $this->ajaxReturn($this->arr);
    }


    /*新增
     *
     * */
    public function add()
    {
        try {
            $model = D($this->model);
            if (!$model->create() || !$model->add()) {
                throw new Exception('新增失败,' . $model->getError());
            }
            $this->arr['msg'] = I('msg') ? I('msg') : '新增成功,' . $model->getLastInsID();
            $this->arr['code'] = 1;
        } catch (Exception $e) {
            $this->arr['msg'] = $e->getMessage();
        }
        $this->ajaxReturn($this->arr);
    }

    /*列表
     *times
     * */
    public function del()
    {
        $a = new $this;
        try {
            $id = I('id');
            if (!$id) {
                throw new Exception('缺少id', 1);
            }
            $model = D($this->model);
            if ($nums = $model->delete($id) === false) {
                throw new Exception('操作失败,' . $model->getError(), 1);
            }
            $this->arr['msg'] = I('msg') ? I('msg') : '删除' . $nums . '条数据';
            $this->arr['code'] = 1;

        } catch (Exception $e) {
            $this->arr['msg'] = $e->getMessage();
        }
        $this->ajaxReturn($this->arr);
    }
}